package com.flower.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.dto.buyer.shop.ShopDto;
import com.flower.domain.dto.buyer.shop.ShopQueryDto;
import com.flower.domain.dto.buyer.product.ProductQueryDto;
import com.flower.domain.vo.buyer.ProductVO;
import com.flower.domain.vo.buyer.ShopVO;
import com.flower.service.ShopService;
import com.flower.utils.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端店铺
 */
@Slf4j
@RestController("adminShop")
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    /**
     * 根据买家ID获取店铺信息
     */
    @GetMapping("/buyer/{buyerId}")
    public AjaxResult getShopByBuyerId(@PathVariable Integer buyerId) {
        ShopVO shop = shopService.getShopByBuyerId(buyerId);
        return AjaxResult.success(shop);
    }

    /**
     * 获取店铺详情
     */
    @GetMapping("/{shopId}")
    public AjaxResult getShopDetail(@PathVariable Integer shopId) {
        ShopVO shop = shopService.getShopDetail(shopId);
        return AjaxResult.success(shop);
    }

    /**
     * 获取店铺商品列表
     */
    @GetMapping("/products")
    public AjaxResult getShopProducts(ProductQueryDto productQueryDto) {
        if (productQueryDto.getShopId() == null) {
            return AjaxResult.error("店铺ID不能为空");
        }
        Page<ProductVO> page = shopService.getShopProducts(productQueryDto);
        return AjaxResult.success(page);
    }

    /**
     * 为购买者创建店铺
     */
    @Log(name = "为购买者创建店铺")
    @PostMapping("/create")
    public AjaxResult createShopForBuyer(@RequestBody ShopDto shopDto) {
        if (shopDto.getBuyerId() == null) {
            return AjaxResult.error("买家ID不能为空");
        }
        
        shopService.createShopForBuyer(shopDto);
        return AjaxResult.success();
    }

    /**
     * 更新店铺信息
     */
    @Log(name = "更新店铺信息")
    @PutMapping("/update")
    public AjaxResult updateShop(@RequestBody ShopDto shopDto) {
        shopService.updateShopByAdmin(shopDto);
        return AjaxResult.success();
    }

    /**
     * 删除店铺
     */
    @Log(name = "删除店铺")
    @DeleteMapping("/{shopId}")
    public AjaxResult deleteShop(@PathVariable Integer shopId) {
        shopService.deleteShopByAdmin(shopId);
        return AjaxResult.success();
    }

    /**
     * 更新店铺状态（营业/休息）
     */
    @Log(name = "更新店铺状态")
    @PutMapping("/status")
    public AjaxResult updateShopStatus(@RequestBody ShopDto shopDto) {
        if (shopDto.getShopId() == null) {
            return AjaxResult.error("店铺ID不能为空");
        }
        if (shopDto.getIsOpen() == null) {
            return AjaxResult.error("营业状态不能为空");
        }
        
        shopService.updateShopStatusByAdmin(shopDto.getShopId(), shopDto.getIsOpen());
        return AjaxResult.success();
    }
} 